package com.mall.sso.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.mall.manager.dao.TbUserMapper;
import com.mall.manager.pojo.TbUser;
import com.mall.manager.pojo.TbUserExample;
import com.mall.pojo.TaotaoResult;
import com.mall.sso.service.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 */

@Service
@Component
@Transactional
public class RegisterServiceImp implements RegisterService {

    @Autowired
    TbUserMapper userMapper;

    @Override
    public TaotaoResult checkUserData(String param, int type) {
        TbUserExample userExample = new TbUserExample();
        TbUserExample.Criteria criteria = userExample.createCriteria();
        if (type == 1){
            criteria.andUsernameEqualTo(param);
            List<TbUser> users = userMapper.selectByExample(userExample);
            if ( users == null || users.size() == 0 ){
                return TaotaoResult.ok(true);
            }else {
                return TaotaoResult.ok(false);
            }
        }else if (type == 2){
            criteria.andPhoneEqualTo(param);
            List<TbUser> users = userMapper.selectByExample(userExample);
            if ( users == null || users.size() == 0 ){
                return TaotaoResult.ok(true);
            }else {
                return TaotaoResult.ok(false);
            }
        }
        return TaotaoResult.ok(true);
    }

    @Override
    public TaotaoResult register(TbUser user) {
        Date date = new Date();
        user.setCreated(date);
        user.setUpdated(date);
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        userMapper.insert(user);
        return TaotaoResult.ok();
    }
}
